package com.sanda.androidcourse.strategy;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sanda.androidcourse.R;

import java.util.List;
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
    private List<Post> postList;
    public PostAdapter(List<Post> posts) {
        this.postList = posts;
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView titleView, bodyView;
        public ViewHolder(View itemView) {
            super(itemView);
            titleView = itemView.findViewById(R.id.textTitle);
            bodyView = itemView.findViewById(R.id.textBody);
        }
    }
    @NonNull
    @Override
    public PostAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,
                                                     int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_post, parent, false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull PostAdapter.ViewHolder holder, int
            position) {
        Post post = postList.get(position);
        holder.titleView.setText(post.getTitle());
        holder.bodyView.setText(post.getBody());
    }
    @Override
    public int getItemCount() {
        return postList.size();
    }
}